﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace genQiang
{
    
    public partial class Form2 : Form
    {
        Queue<long> his = new Queue<long>();
        
        public Form2()
        {
            InitializeComponent();
        }
        int D = 10; //误差
        long bian = 0;
        long dian = 0;
        private void init()
        {
            label1.Visible= false;
            label1.Left = label1.Top = 0;
            label1.Width = this.Width;
            label1.Height= this.Height-10;
            label1.BackColor = Color.Red;
            label1.Text = "等待区域变为绿色点击该区域";
            dian = bian = 0;
            button1.Left=this.Width/2-button1.Width/2;
            button1.Top = this.Height / 2 - button1.Height / 2;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            init();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.BackColor = Color.Green;

            label1.Text = "Click!";
            timer1.Enabled = false;

            bian = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
        }

        Random random = new Random();
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Interval = random.Next(2000,5000);
            timer1.Enabled = true;
            label1.Visible= true;
            button1.Visible = false;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            dian = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
            if (dian <= bian+D || dian == 0 || bian == 0)
            {
                timer1.Enabled = false;
                button1.Visible = true;
                MessageBox.Show("无效!");
            }
            else
            {
                long res = (dian - bian - D);
                DialogResult dialogResult = MessageBox.Show("反应时间:" + res.ToString() + "ms 是否上传成绩?","",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (his.Count >= 10)
                    his.Dequeue();
                his.Enqueue(res);
                button1.Visible = true;

                if(dialogResult== DialogResult.Yes)
                {
                    string httpresp = HttpUtils.HttpGet("cmt/" + MainWin.FYCS + "/" + res.ToString() + "/" + MainWin.youName);
                    MessageBox.Show(httpresp);
                }
            }
            init();
        }

        private void 历史成绩ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string msg="";
            if (his.Count == 0)
            {
                MessageBox.Show("暂无历史成绩", "历史成绩",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            long avg = 0;
            foreach(var i in his.Take(his.Count).Reverse())
            {
                avg += i;
                msg += (i.ToString() + "ms\r\n");
            }
            msg += ("平均值:" + (avg / his.Count).ToString() +"ms");
            MessageBox.Show(msg, "历史成绩");
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainWin.mainWin.Visible = true;
        }

        private void 说明ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("PS\r\n点击开始,等待区域变为绿色立即按下鼠标左键\r\n点击历史成绩可查看最近10次的成绩和平均值\r\n正常人的反应时间大概200ms左右,若能保持低于150ms赶紧去打职业比赛",
                "说明",MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }
    }
}
